package com.wuzuqing.android.eightdiagrams;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.Nullable;

import com.wuzuqing.android.eightdiagrams.model.EightDagramsInfoModel;
import com.wuzuqing.android.eightdiagrams.utils.DataUtils;

public class EightDagramsView extends View {
    private Paint mPaint;

    private String number = "63";
    private String name = "乾为天";
    private String title = "本卦";
    private int yangColor = Color.RED;
    private int yinColor = Color.BLUE;
    private int textColor = 0xff333333;
    private float textSize;
    private int margin = 12;
    private String value = "1-0-1-0-0-0";
    private int dynamicYao = -1;

    public EightDagramsView(Context context) {
        this(context, null);
    }

    public EightDagramsView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EightDagramsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs);
    }

    private int lineHeight;

    private void initAttrs(Context context, AttributeSet attrs) {

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.EightDagramsView);

        array.recycle();

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        lineHeight = (int) TypedValue.applyDimension(1, 8, metrics);
        margin = (int) TypedValue.applyDimension(1, 4, metrics);
        textSize = TypedValue.applyDimension(2, 14, metrics);
        mPaint.setStrokeWidth(lineHeight);
        mPaint.setTextSize(textSize);
    }

    float yaoWidth;
    Rect rect = new Rect();

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        mPaint.getTextBounds("乾", 0, 1, rect);
        yaoWidth = getMeasuredWidth() - margin;
        int height = rect.height() * 3 + margin * 8 + lineHeight * 6;
        setMeasuredDimension(getMeasuredWidth(), height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        canvas.drawColor(0xffacacac);
        mPaint.setColor(textColor);
        int viewWidth = getWidth();
        if (!TextUtils.isEmpty(number)) {
            canvas.drawText(number, (viewWidth - mPaint.measureText(number)) / 2, rect.height()  , mPaint);
        }
        float y = getHeight();
        if (!TextUtils.isEmpty(title)) {
            y = y - rect.height() / 2+10;
            canvas.drawText(title, (viewWidth - mPaint.measureText(title)) / 2 , y, mPaint);
        }
        if (!TextUtils.isEmpty(name)) {
            y = y - rect.height() / 2 - margin-12;
            canvas.drawText(name, (viewWidth - mPaint.measureText(name)) / 2, y, mPaint);
        }
        if (!TextUtils.isEmpty(value)) {
            String[] split = value.split("-");
            y = rect.height() + margin +10 ;
            float x = 0;
            float halfWidth = (yaoWidth - margin) / 2;
            int index = 6;
            for (String s : split) {
                mPaint.setStrokeWidth(lineHeight);
                if ("1".equals(s)) {
                    mPaint.setColor(yangColor);
                    canvas.drawLine(x, y, viewWidth, y, mPaint);
                } else {
                    mPaint.setColor(yinColor);
                    canvas.drawLine(x, y, x + halfWidth, y, mPaint);
                    canvas.drawLine(viewWidth - halfWidth, y, viewWidth, y, mPaint);
                }
                if(dynamicYao > 0 && index == dynamicYao){
                    mPaint.setStrokeWidth(lineHeight/4f);
                    mPaint.setColor(Color.YELLOW);
                    canvas.drawCircle(yaoWidth, y, lineHeight/2, mPaint);
                }
                --index;
                y += (margin + lineHeight);
            }
        }
    }

    public void setValue(EightDagramsInfoModel infoModel) {
        title = infoModel.getTitle();
        name = infoModel.getEightDiagramsName();
        number = String.valueOf(infoModel.getEightDiagramsId());
        value = DataUtils.getEightDagrams(infoModel.getUpperEightDiagrams()).getYaoFlag() + "-" +
                DataUtils.getEightDagrams(infoModel.getDownEightDiagrams()).getYaoFlag();
        dynamicYao = infoModel.getDynamicYao();
        invalidate();
    }
}
